<?php
class Cart extends CI_Controller{
	public function index(){
		$this->template->write_view('left','front_end/layout/left',NULL,TRUE);
		$this->template->write_view('content','front_end/cart/index',NULL,TRUE);
		$this->template->load();
	}
	
	public function add_cart_item(){
		
		
		if($this->Cart_model->validate_add_cart_item() == TRUE){  
	  
	        // Check if user has javascript enabled  
	        if($this->input->post('ajax') != '1'){  
	            redirect('cart'); // If javascript is not enabled, reload the page with new data  
	        }else{  
	            echo 'true'; // If javascript is enabled, return true, so the cart gets updated  
	        }  
	    } 
	    else 
	    	echo 'false';
	}
	
	public function update_cart_item(){
		
		$data = "";
		
		$message = 'false';
		$subtotal = 0;
		$total = 0;
		
		$lang = $this->lang->lang();
		
		$currency = $lang == 'vi' ? 'VNĐ' : 'USD';
		
		$curl = ($lang == 'vi') ? 1 : $this->utility->currency('USD','VND','1');
		if($this->Cart_model->validate_update_cart_item() == TRUE){  
			$message = 'true';
			$subtotal = $this->cart->format_number(round($this->input->post('quantity') * $this->input->post('price') / $curl , 4)) . ' ' .$currency ;
			$total = lang('cart.total') . ': ' .  $this->cart->format_number( round($this->cart->total() / $curl,4) ) . ' ' .$currency ;
	    } 
	    else 
	    	echo 'false';

	    $data = '{"message" : "'.$message.'", "subtotal" : "'.$subtotal.'", "total" : "'.$total.'"}';

	    echo $data;
	    	
	}
	
	public function remove_cart_item(){
		if($this->Cart_model->validate_remove_cart_item() == TRUE){  
			echo 'true';
	    } 
	    else 
	    	echo 'false';
	}
	
	public function show_cart(){
		$this->load->view('front_end/layout/cart');
	}
	
	public function show_items(){
		$this->load->view('front_end/cart/index');
	}
}